//author:kakake
class tsselfpayController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1,
        AppId: 1
    }

    public hisinterface: tshisinterfaceController;
    public service: tsselfpayService;
    public dao: tsselfpayDao;

    constructor() {
        this.data.WorkId=TSLobsterAPI.Program.GetWorkId();

        this.hisinterface = new tshisinterfaceController(1);
        this.service = new tsselfpayService(this.data.WorkId);
        this.dao = new tsselfpayDao(this.data.WorkId);
    }

    

    //获取未缴费处方
    getUnpaidPatPres() {
        let CardType = TSLobsterAPI.Program.RequestQuery("CardType");
        let CardNum = TSLobsterAPI.Program.RequestQuery("CardNum");
        return this.hisinterface.getPatPresUnPay(parseInt(CardType), CardNum);
    }

    //获取处方明细
    getPresDetail() {
        let repno = TSLobsterAPI.Program.RequestQuery("repno");
        return this.hisinterface.getPatPresDetail(repno);
    }

    //获取已缴费处方
    getpaidPatPres() {
        let CardType = TSLobsterAPI.Program.RequestQuery("CardType");
        let CardNum = TSLobsterAPI.Program.RequestQuery("CardNum");
        let StarDate = TSLobsterAPI.Program.RequestQuery("StarDate");
        let EndDate = TSLobsterAPI.Program.RequestQuery("EndDate");

        return this.hisinterface.getPatPresFinished(parseInt(CardType), CardNum, StarDate, EndDate);
    }

    //缴费处方
    balance() {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        return this.service.balance(model);
    }

    //获取处方缴费记录
    getpresrecordlist() {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };

        var PatientId = TSLobsterAPI.Program.RequestQuery('PatientId', '0');
        var userId = TSLobsterAPI.Program.GetMPUserId();
        var workid = TSLobsterAPI.Program.GetWorkId();

        let strsql = "select * from WPMT_PresPayment where workid=@workid and MpUserId=@userId";
        if (parseInt(PatientId) > 0) {
            strsql += " and PatientId=@PatientId"
        }

        var dbparams = {
            workid: workid,
            userId: userId,
            PatientId: PatientId
        }

        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    }
    //获取缴费处方费用明细
    getpresfeelist() {
        let id = TSLobsterAPI.Program.RequestQuery("id", 0);
        let strsql = "select PatName,PayTime,TranserNo,Fee from WPMT_PresPayment where id=@id";
        let head = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { id: id });
        let strsql2 = "select ItemName,UnitPrice,Quantity,Unit from WPMT_FeeDetail where RecordId=@id ";
        let list = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, { id: id });

        return { head: head, list: list };
    }

    /**
     * 自助缴费记录
     * 提供给Web后台查询
     */
    getpresrecordlist_web() {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        var sdate = TSLobsterAPI.Program.RequestQuery('sdate');
        var edate = TSLobsterAPI.Program.RequestQuery('edate');
        var status = TSLobsterAPI.Program.RequestQuery('status');//0全部 1待支付 2已缴费 3已退款 4已取消
        var keyword = TSLobsterAPI.Program.RequestQuery('keyword') || '';
        var workid = TSLobsterAPI.Program.GetWorkId();


        var dbparams = {
            workid: workid,
            sdate: sdate,
            edate: edate,
            status: status,
            keyword: "%" + keyword + "%"
        }

        //PayStatus 1 未支付 2已支付
        //OrderState 订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、
        var strsql = "select * from WPMT_PresPayment  where WorkId=@workid and PatName like @keyword and PayTime BETWEEN @sdate and @edate";
        if (parseInt(status) > 0) {
            if (parseInt(status) == 1) {
                strsql += ' and OrderState=1';
            }
            else if (parseInt(status) == 2) {
                strsql += ' and OrderState=2';
            }
            else if (parseInt(status) == 3) {
                strsql += ' and OrderState=7';
            }
            else if (parseInt(status) == 4) {
                strsql += ' and OrderState=5';
            }
        }

        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    }

    /**
     * 自助缴费记录
     * 费用明细
     */
    getpresfeelist_web() {
        let id = TSLobsterAPI.Program.RequestQuery("id", 0);

        let strsql2 = "select ItemName,UnitPrice,Quantity,Unit from WPMT_FeeDetail where RecordId=@id ";
        let list = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, { id: id });

        return { list: list };
    }

}